package com.example.mybatisplus.video.controller;

import com.example.mybatisplus.video.domain.Video;
import com.example.mybatisplus.video.service.VideoSerivce;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;

/**
 * 视频接口
 *
 * @author zhouxl
 * @DATE 2020/9/27
 * @descriptoin
 */
@Api("视频api")
@Controller
@RequestMapping("/api/video")
public class VideoController {

    @Autowired
    private VideoSerivce videoSerivce;

    /**
     * 上传视频处理
     * 200 - 标识视频上传成功
     * 400 - 标识视频上传失败
     *
     * @param file
     * @return
     */
    @ApiOperation("添加视频信息")
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @ResponseBody
    public Object upload(@RequestParam("file") MultipartFile file) {
        Map<String, Object> result = videoSerivce.upload(file);
        return result;
    }

    /**
     * 通过视频的标识id下载视频信息
     *
     * @param vid
     * @return
     */
    @ApiOperation("下载视频信息")
    @RequestMapping("downVideo")
    public Object downVideo(@RequestParam("vid") String vid, HttpServletResponse response) throws IOException {
        Map<String, Object> map = videoSerivce.queryVideo(vid);
        Video video = (Video) map.get("resultData");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment; filename=" + video.getName());
        ByteArrayInputStream bis = new ByteArrayInputStream(video.getBs());
        IOUtils.copy(bis, response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();
        return null;
    }

    /**
     * video主页面
     *
     * @return
     */
    @RequestMapping("videoMain")
    public String videoMain() {
        return "videoMain";
    }

}
